<template>
	<div>自己付
		<button @click="tap">支付</button>
	</div>
</template>

<script>
	export default {
		onShow() {
			// wx.getSystemInfo({
			// 	success(res) {
			// 		console.log(res, 'res')
			// 	}
			// })
			wx.request({ // 获取ip
				url: 'http://ip-api.com/json',
				success: function(e) {
					console.log('设备信息：', e)
				}
			})
		},
		methods: {
			tap() {
				let getTime = String(Date.now())
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: getTime,
					nonceStr: 'A1B2C3D4E5',
					package: 'prepay_id=wx20180101abcdefg',
					signType: 'MD5',
					paySign: `MD5(appId=wx0c693c7bf8afcdd3&nonceStr=A1B2C3D4E5&package=prepay_id=wx20180101abcdefg&signType=MD5&timeStamp=${getTime}`,

					success(res) {
						console.log(res, 'res')
					},
					fail(e) {
						console.log(e, 'e')
					}
				})
				wx.login({
					success: res => {
						console.log(res, 'code=====');
						const appid = 'wx0c693c7bf8afcdd3'; // 微信小程序appid 
						const secret = 'dcbd19d1b9f65ca7f72839ca1f727428'; // 微信小程序secret 
						//调用request请求api转换登录凭证 
						wx.request({
							url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret +
								'&grant_type=authorization_code&js_code=' + res.code,
							header: {
								'content-type': 'application/json'
							},
							success: function(result) {
								if (result.statusCode === 200) {
									console.log(result, 'openid=======');
									uni.setStorageSync('openid', result.data.openid);
									uni.setStorageSync('session_key', result.data.session_key);
								}

							}
						})
					}
				})
			}
		}
	}
</script>

<style>
</style>